package com.seaboxdata.service.impl;

import com.seaboxdata.dao.DepartmentDao;
import com.seaboxdata.dao.HdfsDao;
import com.seaboxdata.dao.SystemDao;
import com.seaboxdata.dao.TableConfigDao;
import com.seaboxdata.service.HiveDataInfoService;
import com.seaboxdata.service.ShareTableInfoService;
import com.seaboxdata.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @create 2021-01-04 11:39
 **/
@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private HdfsDao hdfsDao;

    @Value("${hive.database}")
    private String database;

    @Value("${hive.base.path}")
    private String hiveBasePath;

    @Autowired
    private DepartmentDao departmentDao;

    @Autowired
    private SystemDao systemDao;

    @Autowired
    private TableConfigDao tableConfigDao;

    @Autowired
    private HiveDataInfoService hiveDataInfoService;

    @Autowired
    private ShareTableInfoService shareTableInfoService;

    @Override
    public Map<String, Object> getStatistics() {
        Map<String, Object> map = new HashMap<>();
        map.put("departmentCount", departmentDao.totalCount());//委办局数量
        map.put("systemCount", systemDao.totalCount());//系统数量
        //获取昨天的日期
        map.put("tableCount", tableConfigDao.totalCount());//接入表总数量
        map.put("totalCount", hiveDataInfoService.getTotalCount(new Date()));//接入数据总条数
        String totalStorage = hdfsDao.getTotalStorage(hiveBasePath + database + ".db");
        BigDecimal bigDecimal = new BigDecimal(totalStorage);
        BigDecimal storage = bigDecimal.divide(BigDecimal.valueOf(1024L * 3L * 1024L * 1024L), 2, BigDecimal.ROUND_HALF_UP);
        map.put("totalStorage", storage + "GB");//占用存储数据量
        //每天门户-接入数据总条数
        map.put("totalIncrement", hiveDataInfoService.getTotalIncrement(new Date()));
        //共享总数据量
        map.put("shareTotalCount",shareTableInfoService.getAllShareTableTotalCount(new Date()) / 10000);
        //共享委办局数量
        map.put("shareTotalUnitCount",shareTableInfoService.getTotalDepartment(new Date()));
        return map;
    }
}
